Skip to content

Method: IndirectMap(Object, Field, UnitOfWorkImpl, Map)

1: /*
2: * JOPA
3: * Copyright (C) 2023 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.adapters;
19:
20: import java.lang.reflect.Field;
21: import java.util.*;
22:
23: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
24:
25: public class IndirectMap<K, V> extends IndirectCollection<Map<K, V>> implements Map<K, V> {
26:
27: private final Map<K, V> internalMap;
28:
29: /**
30: * No-arg constructor to support clone building
31: */
32: IndirectMap() {
33: this.internalMap = new HashMap<>();
34: }
35:
36: public IndirectMap(Object owner, Field f, UnitOfWorkImpl persistenceContext, Map<K, V> referencedMap) {
37: super(owner, f, persistenceContext);
38: this.internalMap = Objects.requireNonNull(referencedMap);
39: }
40:
41: @Override
42: public Map<K, V> unwrap() {
43: return internalMap;
44: }
45:
46: @Override
47: public int size() {
48: return internalMap.size();
49: }
50:
51: @Override
52: public boolean isEmpty() {
53: return internalMap.isEmpty();
54: }
55:
56: @Override
57: public boolean containsKey(Object key) {
58: return internalMap.containsKey(key);
59: }
60:
61: @Override
62: public boolean containsValue(Object value) {
63: return internalMap.containsValue(value);
64: }
65:
66: @Override
67: public V get(Object key) {
68: return internalMap.get(key);
69: }
70:
71: @Override
72: public V put(K key, V value) {
73: V val = internalMap.put(key, value);
74: persistChange();
75: return val;
76: }
77:
78: @Override
79: public V remove(Object key) {
80: V val = internalMap.remove(key);
81: if (val != null) {
82: persistChange();
83: }
84: return val;
85: }
86:
87: @Override
88: public void putAll(Map<? extends K, ? extends V> m) {
89: internalMap.putAll(m);
90: if (!m.isEmpty()) {
91: persistChange();
92: }
93: }
94:
95: @Override
96: public void clear() {
97: if (!isEmpty()) {
98: internalMap.clear();
99: persistChange();
100: }
101: }
102:
103: @Override
104: public Set<K> keySet() {
105: return internalMap.keySet();
106: }
107:
108: @Override
109: public Collection<V> values() {
110: return internalMap.values();
111: }
112:
113: @Override
114: public Set<java.util.Map.Entry<K, V>> entrySet() {
115: return internalMap.entrySet();
116: }
117:
118: @Override
119: public boolean equals(Object o) {
120: if (o instanceof Map) {
121: if (o instanceof IndirectMap) {
122: return internalMap.equals(((IndirectMap) o).internalMap);
123: }
124: return internalMap.equals(o);
125: }
126: return false;
127: }
128:
129: @Override
130: public int hashCode() {
131: return internalMap.hashCode();
132: }
133:
134: @Override
135: public String toString() {
136: return internalMap.toString();
137: }
138: }